/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;
using System.Collections;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_XMLSetAttributes.
	/// </summary>
	public class Block_XMLSetAttributes : Block
	{
		private int _entries;
		private string _nodeHandle;
		private ArrayList attrValList = new ArrayList();

		public override TYPE GetBlockType()
		{
			return TYPE.XMLSetAttributes;
		}

		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_XMLSetAttributes::GetXML Started.");	
			// <XMLSetAttributes name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<XMLSetAttributes name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			
			// ...nodeHandle="HANDLE" >
			xml += " nodeHandle=\"" + Utils.XmlString(_nodeHandle) + "\"";

			xml += ">\r\n";

			// <Inputs>
			xml += GetInputsXML(_blockId);
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			xml += "\t\t\t\t<Attributes>\r\n";
			foreach (KeyVal kv in attrValList)
			{
				string attr = Utils.XmlString(kv.GetKey());
				string val = Utils.XmlString(kv.GetValue());
				xml += "\t\t\t\t\t<XMLSetAttribute name=\"" + attr + "\" value=\"" + val + "\"/>\r\n";
			}
			xml += "\t\t\t\t</Attributes>\r\n";

			// </XMLSetAttributes>
			xml +="\t\t\t</XMLSetAttributes>\r\n";
			return xml;
		}


		/// <summary>
		/// Read all pairs from the data and add to list.
		/// </summary>
		private void ReadEntries()
		{
			for (int i=0; i < _entries; i++)
			{
				int fieldSize = _rawData[_currentPos];
				// Attribute
				_currentPos += 12;
				string attr = Utils.DataToString(_rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(_rawData, _currentPos);
				// Value
				string val = Utils.DataToString(_rawData, _currentPos);
				_currentPos = _currentPos + Utils.DataStringLength(_rawData, _currentPos);
				KeyVal kv = new KeyVal(attr, val);
				attrValList.Add(kv);
				Trace.Debug("Attribute: " + attr + ", value: " + val);
			}
		}


		public Block_XMLSetAttributes(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.

			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, 0));
			outputErrorIds.Add(new BlockOutput(OUTPUT.InvalidHandle, 162));
			outputErrorIds.Add(new BlockOutput(OUTPUT.Failed_2, 141));

			Trace.Debug("Block_XMLSetAttributes created.");


			_entries = rawData[_currentPos + 8];

			_currentPos += 12;

			// node handle
			_nodeHandle = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			ReadEntries();

			Trace.Debug("Node handle: " + _nodeHandle);
		}
	}
}
